package cn.portx.view;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.bstek.dorado.annotation.DataProvider;

@Component
public class BoxDynamicPR {

	@Autowired
	private RestTemplate restTemplate;

	@DataProvider
	@Transactional
	public List<Map<String, Object>> loadBoxDynamic(String cntNo) {

		if (StringUtils.isBlank(cntNo)) {
			return Collections.EMPTY_LIST;
		}

		String url = "http://api.portx.cn/commonapi/container/dynamic/?";

		url = StringUtils.replaceFirst(url, "\\?", cntNo);

		Map<String, Object> resultMap = this.restTemplate.getForObject(url, Map.class);

		int code = Integer.parseInt(resultMap.get("code").toString());
		if (code == 0) {
			//成功
			List<Map<String, Object>> resultList = (List<Map<String, Object>>) resultMap.get("result");
			return resultList;
		} else {
			String errorMsg = (String) resultMap.get("msg");
			throw new RuntimeException(errorMsg);
		}
	}
}
